package com.chezhujie.www.controller;

import com.chezhujie.www.constant.PageInfo;
import com.chezhujie.www.domain.*;
import com.chezhujie.www.enums.OrderStatus;
import com.chezhujie.www.service.*;
import com.xy.core.page.Pagination;
import com.xy.core.utils.OrderNumUtils;
import com.xy.core.utils.RandomUtil;
import com.xy.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/orderApi")
public class CzjxyOrderApiController extends BaseController<Czjorder> {
	
    private  final Logger log = LoggerFactory.getLogger(CzjxyOrderApiController.class);
    
    @Autowired
    private CzjorderService czjorderService;
    
    @Autowired
    private CzjorderdetailService czjorderdetailService;
    
    @Autowired
    private CzjactdetailService czjactdetailService;
    
    @Autowired
    private CzjactivityService czjactivityService;
    @Autowired
    private CzjexpressService czjexpressService;
    
    @Autowired
    private CzjbusinessService czjbusinessService;
    
    @Autowired
    private CzjfestivalactService czjfestivalactService;
    
    /**
     * 
     * 方法名：list 显示我的订单 自有商品 
     * 创建人：RUI
     * 时间：2017年6月7日-下午3:01:17 
     * @param pageNo
     * @param pageSize
     * @param userinfoId
     * @param req
     * @param resp
     * @param model
     * @throws Exception void
     * @exception 
     * @since  1.0.0
     */
    @RequestMapping(value="list")
    public void list(Integer pageNo,Integer pageSize,@RequestParam(required=true) Integer userinfoId,HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
    	try {
			if(null==pageNo){
				pageNo=PageInfo.DEFAULT_PAGENO;
			}
			if(null==pageSize){
				pageSize=PageInfo.DEFAULT_PAGESIZE;
			}
			if(null==userinfoId){
				jsonResult(0, "155", "用户为空", "", resp);
				return ;
			}
			Map<String, Object> params=new HashMap<String, Object>();
				params.put("userinfoId", userinfoId);
			Pagination pagination=czjorderService.listOrderDetail(pageNo, pageSize, params, model);
			jsonResult(1, "", "", pagination, resp);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0, "136", "显示我的订单错误", "", resp);
		}
    	
    	
    }
    
    /**
     * 
     * 方法名：freeSignup 预定免费活动 不走微信支付
     * 创建人：RUI
     * 时间：2017年6月12日-下午2:44:43 
     * @param id
     * @param req
     * @param resp
     * @param model
     * @throws Exception void
     * @exception 
     * @since  1.0.0
     */
    
    @RequestMapping("/freeSignup")
    public void freeSignup(@RequestParam(required=true) Integer userinfoId,
    		String businessId,
    		@RequestParam(required=true) String festivalActId,
    		@RequestParam(required=true) Integer num,
    		HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
    	
    	try {
    		
    		Map<String,Object> map=new HashMap<String, Object>();
    				map.put("festivalActId", festivalActId);
    				Czjfestivalact czjfestivalact= czjfestivalactService.getCzjfestivalact(map);
    		if(null==czjfestivalact){
    			jsonResult(0, "163", "活动不存在", "", resp);
    			return ;
    		}
    		Date endtime=czjfestivalact.getEndTime();
    		if(null!=endtime){ //如果结束时间减去当前时间 为 正数表示可以预定 否则
    			long times=endtime.getTime() - new Date().getTime();
    			if(times<0){
    				jsonResult(0, "164", "活动已经结束,不可以报名", "", resp);
    				return ;
    			}
    		}
    		
    		
    		Czjactivity czjactivity=new Czjactivity();
			String code= OrderNumUtils.getOrderNo(1);
				czjactivity.setActivityNum(code);
				if(StringUtils.isNotBlank(businessId)){
					
					czjactivity.setBusinessId(businessId);
				}
				czjactivity.setDel(1);
				czjactivity.setFree(0);
				czjactivity.setHide(1);
				czjactivity.setStatus(4);//未使用
				czjactivity.setTotalAmount(new java.math.BigDecimal(0));
				czjactivity.setUserinfoId(userinfoId);
				czjactivity.setCreatetime(new Date());
				czjactivity.setHide(1);
				czjactivity.setRandStr(RandomUtil.getRandomString(6));
				
			Czjactdetail czjactdetail=new Czjactdetail();
			czjactdetail.setActivityNum(code);
			czjactdetail.setBuyingPrice(new java.math.BigDecimal(0));
			czjactdetail.setCreatetime(new Date());
			czjactdetail.setFestAmount(new java.math.BigDecimal(0));
				czjactdetail.setFestivalActId(festivalActId);
				czjactdetail.setNun(num);
			
				czjactivityService.insertActivity(czjactivity);
				czjactdetailService.insertCzjactdetail(czjactdetail);
			
			jsonResult(1, "", "", "", resp);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0, "139", "预定免费活动失败", "", resp);
		}
    		
    		
    	
    }
    
    @RequestMapping("/detail")
    public void detail(@RequestParam(required=true) String id,HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
    	
    	
    	
    }
    
    /**
     * 获得物流信息
     * 方法名：getExpressBycode
     * 创建人：RUI
     * 时间：2017年7月6日-下午2:06:40 
     * @param orderCode
     * @param req
     * @param resp
     * @param model
     * @throws Exception void
     * @exception 
     * @since  1.0.0
     */
    @RequestMapping("/getExpressBycode")
    public void getExpressBycode(@RequestParam(required=true) String orderCode,HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
    	
    	try {
			Map<String,Object> map=new HashMap<String, Object>();
			
				map.put("orderCode", orderCode);
			Czjexpress czjexpress=czjexpressService.getExpressByOrdercode(map);
			jsonResult(1, "", "", czjexpress, resp);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0, "154", "查询商品物流信息失败", "", resp);
		}
    	
    }
    
    
    
    /**
     * 
     * 方法名：getBusinPhone 获得商家电话
     * 创建人：RUI
     * 时间：2017年7月6日-下午2:14:47 
     * @param businessId
     * @param req
     * @param resp
     * @param model
     * @throws Exception void
     * @exception 
     * @since  1.0.0
     */
    @RequestMapping("/getBusinPhone")
    public void getBusinPhone(@RequestParam(required=true) String businessId,HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
    	
    	try {
    		
			
    		Czjbusiness czjbusiness=czjbusinessService.queryById(businessId);
			jsonResult(1, "", "", czjbusiness, resp);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0, "156", "获取商家信息错误", "", resp);
		}
    	
    }
    
    /**
     * 
     * 
     * 方法名：receiptOrder 确定收货
     * 创建人：RUI
     * 时间：2017年7月6日-下午2:16:53 
     * @param businessId
     * @param req
     * @param resp
     * @param model
     * @throws Exception void
     * @exception 
     * @since  1.0.0
     */
    @RequestMapping("/receiptOrder")
    public void receiptOrder(@RequestParam(required=true) String id,HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model) throws Exception {
    	
    	try {
			
    		Czjorder czjorder=czjorderService.queryById(id);
    		if(null!=czjorder){
    				czjorder.setId(id);
        			czjorder.setStatus(OrderStatus.FIN.getIndex());
        			czjorder.setUpdatetime(new Date());
        		czjorderService.update(czjorder);
    			jsonResult(1, "", "", "", resp);
    			
    		}else{
    			jsonResult(0, "158", "没有订单，非法请求 ", "", resp);
    		}
    			
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0, "157", "确定收货错误", "", resp);
		}
    	
    }

}
